package atm.com.service.cashdispenser;

import atm.com.mapper.CashdispenserMapper;
import atm.com.pojo.Cashdispenser;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * CashDispenserServiceImpl class
 * @author di zhang
 * @date 2021/8/24
 */
@Service
public class CashDispenserServiceImpl implements CashDispenserServiceInf {
    @Autowired
    private CashdispenserMapper cashdispenserMapper;

    @Override
    public BigDecimal GetCashDispenserBalance(Cashdispenser cashDispenser) {
        QueryWrapper<Cashdispenser> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(Cashdispenser::getCashDispenserId, cashDispenser.getCashDispenserId());
        return   cashdispenserMapper.selectOne(queryWrapper).getCashDispenserMoney();
    }

    @Override
    public List<Cashdispenser> ListCashDispenser() {
        QueryWrapper<Cashdispenser> queryWrapper=new QueryWrapper <>();
        return cashdispenserMapper.selectList(queryWrapper);
    }

    @Override
    public Integer UpdateCashDispenserBalance(Cashdispenser cashdispenser, BigDecimal money) {
        UpdateWrapper<Cashdispenser> updateWrapper =new UpdateWrapper <>();
        updateWrapper.lambda().eq(Cashdispenser::getCashDispenserId,cashdispenser.getCashDispenserId());
        cashdispenser.setCashDispenserMoney(cashdispenser.getCashDispenserMoney().add(money));
        return cashdispenserMapper.update(cashdispenser,updateWrapper);
    }
    @Override
    public Integer TakeCashDispenserBalance(Cashdispenser cashdispenser, BigDecimal money) {
        UpdateWrapper<Cashdispenser> updateWrapper =new UpdateWrapper <>();
        updateWrapper.lambda().eq(Cashdispenser::getCashDispenserId,cashdispenser.getCashDispenserId());
        cashdispenser.setCashDispenserMoney(cashdispenser.getCashDispenserMoney().subtract(money));
        return cashdispenserMapper.update(cashdispenser,updateWrapper);
    }

}
